﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using ELMWCFServiceClass.ELMWCF;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Composite.Modularity;
using System.Collections.ObjectModel;
using ELMApplication.Common.Util;
using ELMApplication.Common.Const;
using System.Linq;

namespace SettingModule.Views.ELMListConfigView
{
    public class ELMListConfigPresentationModel:IELMListConfigPresentationModel
    {

        private ELMServiceClient _domaincontext;
        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager regionManager;
        private readonly IModuleManager moduleManager;

        public ELMListConfigPresentationModel(IELMListConfigView view, ELMServiceClient domaincontext, IEventAggregator eventAggregator, IRegionManager regionManage, IModuleManager moduleManager)
        {
            this._domaincontext = domaincontext;
            this._eventAggregator = eventAggregator;
            this.regionManager = regionManage;
            this.moduleManager = moduleManager;
            this.View = view;
            view.SetModel(this);
           
        }





        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

        #region IELMListConfigPresentationModel Members

        public IELMListConfigView View
        {
            get;
            set;
        }

        #endregion

        #region IELMListConfigPresentationModel Members


        public void InitialData()
        {
            BusyMessage.Show("数据读取中...");
            _domaincontext = new ELMServiceClient();
           
            _domaincontext.GetAllContentTypeAsync();
            _domaincontext.GetAllContentTypeCompleted += (sender, data) =>
                {
                    if (data.Error == null)
                    {
                        AllContentTypes = data.Result;
                        NotifyPropertyChanged("AllContentTypes");
                        _domaincontext.GetAllELMListsAsync();
                        _domaincontext.GetAllELMListsCompleted += (sender1, data1) =>
                        {
                            if (data1.Error == null)
                            {
                                ListDataSource = data1.Result;
                                BusyMessage.Close();
                            }
                            else
                            {
                                throw data1.Error;
                            }
                        };

                    }
                    else
                    {
                        throw data.Error;
                    }
                };
        }

        #endregion

        #region IELMListConfigPresentationModel Members

        private ObservableCollection<ELMList> _listDataSource;
        public ObservableCollection<ELMList> ListDataSource
        {
            get
            {
                return _listDataSource;
            }
            set
            {
                _listDataSource = value;
                NotifyPropertyChanged("ListDataSource");
            }
        }

        #endregion

        #region IELMListConfigPresentationModel Members

        private ELMList _currentData;
        public ELMList CurrentData
        {
            get
            {
                return _currentData;
            }
            set
            {
                _currentData = value;
                NotifyPropertyChanged("CurrentData");
            }
        }

        #endregion

        #region IELMListConfigPresentationModel Members


        public void SaveData()
        {
            BusyMessage.Show();
            _domaincontext = new ELMServiceClient();
            _domaincontext.UpdateELMListAsync(CurrentData);
            _domaincontext.UpdateELMListCompleted += (sender, data) =>
                {
                    if (data.Error == null)
                    {
                        InitialData();
                        BusyMessage.Close();
                    }
                    else
                    {
                        throw data.Error;
                    }
                };
        }

        #endregion

        #region IELMListConfigPresentationModel Members


        public void ShowDetail(PrivegeTypes ptype)
        {
            DetailPrivege = ptype;
            //CurrentData = ListDataSource.First(c => c.ELMListId == new Guid(DataKey));
            CW_ELMListDetial cw = new CW_ELMListDetial(this);
            cw.Show();
        }

        public void ShowNewDetail()
        {
            DetailPrivege = PrivegeTypes.New;
            CurrentData = new ELMList();
            CurrentData.ELMListId = Guid.NewGuid();
            CW_ELMListDetial cw = new CW_ELMListDetial(this);
            cw.Show();
        }

        #endregion

        #region IELMListConfigPresentationModel Members

        private ObservableCollection<ContentType> _allContentTypes;
        public ObservableCollection<ContentType> AllContentTypes
        {
            get
            {
                return _allContentTypes;
            }
            set
            {
                _allContentTypes = value;
                NotifyPropertyChanged("AllContentTypes");
            }
        }

        #endregion

        #region IELMListConfigPresentationModel Members


        public PrivegeTypes DetailPrivege
        {
            get;
            set;
        }

        #endregion
    }
}
